package org.csu.mall.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;

@Getter
//@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UniformResponse<T> implements Serializable {

    private int status;
    private String msg;
    private T data;

    private UniformResponse(int status){
        this.status = status;
    }
    private UniformResponse(int status, String msg){
        this.status = status;
        this.msg = msg;
    }
    private UniformResponse(int status, String msg, T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
    private UniformResponse(int status, T data){
        this.status = status;
        this.data = data;
    }

    @JsonIgnore //json序列化时忽略自带的不必要的信息
    public boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }
    @JsonIgnore //json序列化时忽略自带的不必要的信息
    public boolean isLogin(){
        return this.status == ResponseCode.UserExist.getCode();
    }

    public static <T> UniformResponse<T> createForSuccess(){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> UniformResponse<T> createForSuccessMessage(String msg){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> UniformResponse<T> createForSuccess(T data){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> UniformResponse<T> createForSuccess(String msg, T data){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> UniformResponse<T> createForError(){
        return new UniformResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDescription());
    }
    public static <T> UniformResponse<T> createForErrorMessage(String msg){
        return new UniformResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }
    public static <T> UniformResponse <T> createForErrorCodeMessage(int code,String msg){
        return new UniformResponse<T>(code,msg);
    }
    public static <T> UniformResponse<T> logining(){
        return new UniformResponse<T>(ResponseCode.UserExist.getCode(),ResponseCode.UserExist.getDescription());
    }
    public static <T> UniformResponse<T> notLogining(){
        return new UniformResponse<T>(ResponseCode.UserEmpty.getCode(),ResponseCode.UserEmpty.getDescription());
    }
}
